package com.earnings.okhttputils.utils.car.ui;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.DiscoverData;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpArrayCallback;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class DiscoverActivity extends GodLeftHandBaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private GodBaseAdapter adapter;
    private GodArrayList arrayList;
    private SwipeRefreshLayout swiperefresh;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_discover;
    }

    @Override
    protected void init() {
        setTitle("发现");
        swiperefresh=getView(R.id.swiperefresh);
        swiperefresh.setOnRefreshListener(this);
        arrayList = new GodArrayList();
        adapter = new GodBaseAdapter<DiscoverData>(R.layout.item_discover, arrayList) {
            @Override
            public void onBind(GodViewHolder holder, int position, DiscoverData data) {
                holder.setText(R.id.title_tv, data.getTitle());
                holder.setText(R.id.time_tv, data.getInputtime());
                Glide.with(DiscoverActivity.this).load(data.getThumb()).into((ImageView) holder.getView(R.id.img_view));
            }

            @Override
            public void OnItemClickListener(View view, int position, DiscoverData data) {
                mIntent.putExtra("url",data.getUrl());
                skipActivity(DiscoverDetailsActivity.class);

            }
        };
        setRecyclerViewLayoutManager(R.id.recyclerview, new LinearLayoutManager(this)).setAdapter(adapter);
        http();
        monitorRecyclerView();

    }
    private void monitorRecyclerView(){
        RecyclerView recyclerView=getView(R.id.recyclerview);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            //用来标记是否正在向最后一个滑动
            boolean isSlidingToLast = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();

                    // 判断是否滚动到底部，并且是向右滚动
                    if (lastVisibleItem == (totalItemCount - 1) && isSlidingToLast) {
                        //加载更多功能的代码
                        http();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //dx用来判断横向滑动方向，dy用来判断纵向滑动方向
                if (dy > 0) {
                    //大于0表示正在向右滚动
                    isSlidingToLast = true;
                } else {
                    //小于等于0表示停止或向左滚动
                    isSlidingToLast = false;
                }
            }
        });

    }

    private int page=1;
    private void http() {
        showProgress();
        Map params = new HashMap();
        params.put("catid", "7");
        params.put("pagesize", "10");
        params.put("page", page+"");
        CarHttp http = new CarHttp("news", params);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(http).build().execute(new GodHttpArrayCallback<DiscoverData>() {
            @Override
            public void onSuccess(List<DiscoverData> datas, String msg, String response) {
                page++;
                arrayList.addAll(datas);
                swiperefresh.setRefreshing(false);
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                ToastUtils.showToast(DiscoverActivity.this, "网络异常");
                swiperefresh.setRefreshing(false);
                dismissProgress();
            }
        });

    }


    @Override
    public void onRefresh() {
        page=1;
        http();
    }
}
